<?php
	namespace Annoubas\BaseBundle\Controller;
    
	use Symfony\Component\DependencyInjection\ContainerAware;
	use Symfony\Component\HttpFoundation\RedirectResponse;
	use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Symfony\Component\HttpFoundation\Response;
	
	use Annoubas\BaseBundle\Entity\Rubs;
	
	class DataHomeController extends ContainerAware {
		
        /**
         *
         * @return type 
         */
		public function listAction() {
			$em = $this->container->get('doctrine')->getEntityManager();
            
            $sql = "select o from AnnoubasBaseBundle:Orgs o order by o.nbVisit desc";
            $query = $em->createQuery($sql);
            $query->setMaxResults(10);
            $t_orgs = $query->getResult();
            
            $letters = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
            
            $sql = "select c from AnnoubasBaseBundle:Cities c order by c.name asc";
            $query = $em->createQuery($sql);
			$cities = $query->getResult();
            
            $sql = "select sr from AnnoubasBaseBundle:SubRubs sr order by sr.name asc";
            $query = $em->createQuery($sql);
			$subrubs = $query->getResult();
            
			return $this->container->get('templating')->renderResponse('AnnoubasBaseBundle:Default:index.html.twig',array(
				'cities' => $cities, 'subrubs' => $subrubs, 't_orgs' => $t_orgs, 'letters' => $letters)
			);
		}
        
        public function forgeHtmlOrgsContent($orgs) {
            $i = 0;
            $html = '<div style="font-size: 16px;  color:#cd0a0a; padding-top: 13px;">Aucune donnée correspondante n\'a été retrouvé :(</div>';
            $len = sizeof($orgs);
            if ($len > 0) {
                $html = "";
                foreach ($orgs as $org) {
                    $i ++;
                    $html .= '<p>';
                    $html .= '<img id="img'.$org->getIdo().'" src="bundles/annoubasbase/logo/logo_'.$org->getLogo().'.jpg" alt="" style="float: left; border: 1px solid #ccc; padding: 2px;"/>';
                    $html .= '<div class="details_apperxu">';
                    $html .= '<a href="#test" id="go'.$org->getIdo().'" rel="leanModal" onclick="showDetails('.$org->getIdo().'); return false;" class="lien_c0" rel="'.$org->getIdo().'">'.$org->getName().'</a><br/>';
                    $html .= '<div style="font-size: 12px;">';
                    $html .= '<span id="data'.$org->getIdo().'">';
                    if ($org->getBp() != null) {
                        $html .= 'B.P : '.$org->getBp();
                    }
                    if ($org->getTel() != null) {
                        $html .= ' - Tel : '.$org->getTel();
                    }
                    if ($org->getFax() != null) {
                        $html .= ' - Fax : '.$org->getFax();
                    }
                    if ($org->getEmail() != null) {
                        $html .= ' <a href="#" class="lien_c" rel="'.$org->getEmail().'">'.$org->getEmail().'</a>';
                    }
                    $html .= '</span><br/></div>';
                    $html .= '<div id="org'.$org->getIdo().'">';
                    $html .= '<div class="loading" id="loading'.$org->getIdo().'"></div>';
                    $html .= '</div>';
                    $html .= '<a href="http://www.'.$org->getWebs().'" target="_blank" class="lien_c">Site Web</a> - ';
                    $html .= '<a href="#" class="lien_c">Voir sur la carte</a>';
                    $html .= '</div>';
                    $html .= '</p>';
                    $html .= '<div class="line"></div>';
                }
                if ($i >= 10) {
                    $html .= '<div id="data_result_more2">';
                    $html .= '<div class="loading" id="loading2"></div>';
                    $html .= '<input type="button" value="Afficher plus" onclick="getMoreOrgs(2); return false;"/>';
                    $html .= '</div>';
                }
            }
            return $html;
        }
        
        public function moreOrgsAction() {
            $request = $this->container->get('request');
            $em = $this->container->get('doctrine')->getEntityManager();
            $offset = $request->request->get('offset');
            if($request->isXmlHttpRequest()) {
                $sql = "select o from AnnoubasBaseBundle:Orgs o order by o.name asc";
                $query = $em->createQuery($sql);
                $query->setMaxResults(10*$offset);
                $query->setFirstResult(10*($offset - 1));
                $orgs = $query->getResult();
                
                return new Response($this->forgeHtmlOrgsContent($orgs));
            }
        }
        
        /**
         *
         * @return \Symfony\Component\HttpFoundation\Response 
         */
        public function apperxuAction() {
            
            $request = $this->container->get('request');
            $em = $this->container->get('doctrine')->getEntityManager();
            $ido = $request->query->get('ido');
            if($request->isXmlHttpRequest()) {
                
                $sql = "select o from AnnoubasBaseBundle:Orgs o where o.ido = ".$ido;
                $query = $em->createQuery($sql);
                $org = $query->getSingleResult();
                
                $html = '<div style="margin: 5px 5px 5px 5px">';
                $html .= '<img src="bundles/annoubasbase/logo/logo_'.$org->getLogo().'.jpg" alt="" style="float: left;"/>';
                $html .= '<a href="#" class="title_apperxu">'.$org->getName().'</a>';
                $html .= '<div class="details_apperxu">';
                $html .= $org->getDescrip().'<br/>';
                $html .= 'B.P : '.$org->getBp().'<br/>';
                $html .= 'Tel : '.$org->getTel().'<br/>';
                $html .= 'Fax : '.$org->getFax().'<br/>';
                $html .= $org->getEmail().'<br/>';
                $html .= '<a href="http://www.'.$org->getWebs().'" class="lien_c" target="_blank">www.'.$org->getWebs().'</a><br/>';
                $html .= '</div>';
                $html .= '</div>';
                
                return new Response($html);
            }
        }
        
        /**
         * methode permettant d'avoir les details d'une organisation
         * @return \Symfony\Component\HttpFoundation\Response 
         */
        public function detailsAction() {
            $request = $this->container->get('request');
            $em = $this->container->get('doctrine')->getEntityManager();
            $ido = $request->request->get('ido');
            
            if($request->isXmlHttpRequest()) {
                
                /*$sql = "update AnnoubasBaseBundle:Orgs o set o.nbVisit = (o.nbVisit + 1) where o.ido = ".$ido;
                $query = $em->createQuery($sql);*/
                
                $org = $em->getRepository('AnnoubasBaseBundle:Orgs')->find($ido);
                $org->setNbVisit($org->getNbVisit() +1);
                $em->persist($org);
                $em->flush();
                
                $sql = "select o from AnnoubasBaseBundle:Orgs o where o.ido = ".$ido;
                $query = $em->createQuery($sql);
                $org = $query->getSingleResult();
                
                $html = '<span style="font-size:16px; font-weight:bold; color:#cd0a0a;">'.$org->getName().'</span>';
                $html .= '<div class="line1"></div>';
                $html .= '<img src="bundles/annoubasbase/logo/logo_'.$org->getLogo().'.jpg" alt="" style="float: left; border: 1px solid #ccc; padding: 2px;margin-right:10px;"/>';
                if ($org->getBp() != null) {
                    $html .= 'BP : '.$org->getBp().'<br/>';
                }
                $telexist = false;
                if ($org->getTel()) {
                    $html .= 'Tel : '.$org->getTel();
                    $telexist = true;
                }
                if ($org->getFax()) {
                    if ($telexist) $html .= ' / ';
                    $html .= 'Fax : '.$org->getFax();
                }
                $html .= '<br/>';
                if ($org->getEmail()) {
                    $html .= '<a href="#" class="lien_c">'.$org->getEmail().'</a><br/>';
                }
                if ($org->getWebs()) {
                    $html .= '<a href="http://www.'.$org->getWebs().'" target="_blank" class="lien_c">'.$org->getWebs().'</a>';
                }
                $html .= '<div style="margin-left: 76px; margin-top: 10px;">';
                $html .= '<span style="font-weight:bold;">'.$org->getDg().'</span><br/>';
                $html .= $org->getCity()->getName().' : ';
                $html .= $org->getAdress().'<br/>';
                /*$html .= $org->getSubrub()->getRub()->getName().', '*/$html .= $org->getSubrub()->getName().'<br/>';
                $html .= $org->getDescrip().'<br/>';
                $html .= '</div>';
                $html .= '<div class="line1"></div>';
                $html .= '<i style="float: right;">Visité '.$org->getNbVisit().' fois</i>';
                
                return new Response($html);
            }
        }
        
        public function searchAction() {   
			$orgs = "";
            $em = $this->container->get('doctrine')->getEntityManager();
            
            $request = $this->container->get('request');
            
            if($request->isXmlHttpRequest()) {
                $motcle = $request->request->get('motcle');
                $city = $request->request->get('city');
                $subrub = $request->request->get('subrub');
                $letter = $request->request->get('letter');

                $select = "select o from AnnoubasBaseBundle:Orgs o ";
                $where = "where o.name LIKE :motcle ";
                if ($motcle != '') {
                    /* récupère des organismes en fonction de la ville 
                        * et de sous rubrique et du mot clé
                        */
                    $where = "where o.name LIKE :motcle AND o.city = :city AND o.subrub = :subrub";
                    $query = $em->createQuery($select.''.$where.' order by o.name asc')
                                ->setParameter('motcle', $motcle.'%')
                                ->setParameter('city', $city)
                                ->setParameter('subrub', $subrub);
                    $query->setMaxResults(10);
                    $orgs = $query->getResult();
                } else {
                    $where = "where o.name LIKE :motcle AND o.subrub = :subrub AND o.city = :city";
                    $query = $em->createQuery($select.''.$where.' order by o.name asc')
                                ->setParameter('motcle', $letter.'%')
                                ->setParameter('subrub', $subrub)
                                ->setParameter('city', $city);
                    $query->setMaxResults(10);
                    $orgs = $query->getResult();
                }
                return new Response($this->forgeHtmlOrgsContent($orgs));
            }
        }
	}